import React, { memo, useEffect, useState, } from "react";
import Taro, { useDidShow } from "@tarojs/taro";
import { MineHeader, MineCenter, DownLoadApp } from "@/components";
import { Image, Text, View } from "@tarojs/components";
import { useDispatch } from "react-redux";
import { checkWalletBindContent } from "@/services/api/wallet";
import './index.scss'





const Mine = () => {
  const dispatch = useDispatch()
  const [userInfo, setUserInfo]: any = useState({})
  const [walletInfoFlag, setwalletInfoFlag]: any = useState({})
  const [showQrCode, setShowQrCode] = useState(false)

  useEffect(() => {
    // 如果用户首次进入
  }, [])

  const getUserDetaill = async () => {
    dispatch({
      type: 'certified/asyncGetUserDetail',
      callback: (res1) => {
        if (res1.code === 200) {
          setUserInfo(res1.data);
        }
      }
    })
  }

  /**
   * 获取钱包信息
   */
  const getWalletInfoFlag = async () => {
    checkWalletBindContent().then(res1 => {
      if (res1.code === 200) {
        console.log(res1)
        setwalletInfoFlag(res1.data);
      }
    })
  }

  /**
  * 对应 onShow
  */
  useDidShow(() => {
    let token = Taro.getStorageSync('auth-token') ?? ''
    if (!token) {
      Taro.reLaunch({
        url: "/login/login/index"
      })
    } else {
      getUserDetaill()
      getWalletInfoFlag()
    }
  })

  /**
   * 去钱包页面
   */
  const gotoWallet = () => {
    Taro.navigateTo({ url: '/wallet/index' });

  }

  /**
   * 去钱包app提示页面
   */
  const gotoAPPWalletTips = () => {
    // Taro.navigateTo({
    //   url: '/mineDetail/webView/index?src=https://www.hzddyy.com/activity/download/yidaotou'
    // })
    setShowQrCode(true)

  }




  return (
    <>
      <View className='page'>
        <MineHeader />
        {
          userInfo?.userChainInfo?.walletAddress
            ?
            <View className='mine-wallet' >
              <View className='wallet-icon'>
                <Image className='dy-shield' src={require('@/assets/icons/mine/daoyi_shield.svg')}></Image>
              </View>
              <Image className='wallet-sm' src={require(`@/assets/images/${walletInfoFlag.certiStatusFlag ? "mine-wallet-sm" : "mine-wallet-no-sm"}.png`)}></Image>
              <Image className='wallet-ei' src={require(`@/assets/images/${walletInfoFlag.eligibleInvestFlag ? "mine-wallet-ei" : "mine-wallet-no-ei"}.png`)}></Image>
            </View>
            :
            <View className='mine-wallet nowallet' onClick={gotoAPPWalletTips}>
              <View className='wallet-icon nowallet'>
                <Image className='dy-shield nowallet' src={require('@/assets/icons/mine/daoyi_nowallet_shield.svg')}></Image>
              </View>
              <View className='downapp nowallet'>
                <Text className='name'>下载一道投APP开启保护</Text>
                <Image src={require('@/assets/icons/mine/mine_wallet_arrow_right.svg')}></Image>
              </View>
            </View>
        }
        <MineCenter userInfo={userInfo} />
        <DownLoadApp downLoadUrl='https://www.hzddyy.com/activity/download/yidaotou' show={showQrCode} close={() => setShowQrCode(false)} />

      </View>

    </>

  );
};

export default memo(Mine);
